package com.cs456.TeamBobcat;

import java.io.IOException;

import com.cs456.TeamBobcat.database.MyDB;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.media.MediaPlayer;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.os.Environment;
import android.text.format.Time;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class WhatDidTheySay extends Activity implements OnClickListener {
	private static final String LOG_TAG = "AudioRecorder";
	private static String mFileName = null;
	

	// private RecordButton mRecordButton = null;
	private MediaRecorder mRecorder = null;

	// private PlayButton mPlayButton = null;
	private MediaPlayer mPlayer = null;

	private boolean mStartRecording = true;
	private boolean mStartPlaying = true;

	protected String _path;
	protected Intent _result;
	ProgressDialog _progressDialog;

	Thread _computeResult;

	private void onRecord(boolean start) {
		if (start) {
			startRecording();
		} else {
			stopRecording();
		}
	}

	private void onPlay(boolean start) {
		if (start) {
			startPlaying();
		} else {
			stopPlaying();
		}
	}

	private void startPlaying() {
		mPlayer = new MediaPlayer();
		try {
			mPlayer.setDataSource(mFileName);
			mPlayer.prepare();
			mPlayer.start();
		} catch (IOException e) {
			Log.e(LOG_TAG, "prepare() failed");
		}
	}

	private void stopPlaying() {
		mPlayer.release();
		mPlayer = null;
	}

	private void startRecording() {
		mRecorder = new MediaRecorder();
		mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
		mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
		mRecorder.setOutputFile(mFileName);
		mRecorder.setOutputFile(mFileName);
		mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);

		try {
			mRecorder.prepare();
		} catch (IOException e) {
			Log.e(LOG_TAG, "prepare() failed");
		}

		mRecorder.start();
	}

	private void stopRecording() {
		mRecorder.stop();
		mRecorder.release();
		mRecorder = null;
	}

	public WhatDidTheySay() {
		Time now = new Time();	
		now.setToNow();
		mFileName = Environment.getExternalStorageDirectory().getAbsolutePath();
		mFileName += "/";
		mFileName += now.format2445();
		mFileName += "wdts.3gp";
		_path = mFileName;

	}

	@Override
	public void onCreate(Bundle icicle) {
		super.onCreate(icicle);
		_result = new Intent(this, WhatDidTheySayResult.class);

		setContentView(R.layout.whatdidtheysay);
		

		Button RecordButton = (Button) findViewById(R.id.recordButton);
		Button PlayButton = (Button) findViewById(R.id.playButton);
		Button SubmitButton = (Button) findViewById(R.id.submitButton);

		RecordButton.setOnClickListener(this);
		PlayButton.setOnClickListener(this);
		SubmitButton.setOnClickListener(this);
	}

	public void onClick(View view) {
		Button RecordButton = (Button) findViewById(R.id.recordButton);
		Button PlayButton = (Button) findViewById(R.id.playButton);
		switch (view.getId()) {
		case R.id.recordButton:
			onRecord(mStartRecording);
			if (mStartRecording) {
				RecordButton.setText("Stop Recording");
			} else {
				RecordButton.setText("    Record    ");
			}
			mStartRecording = !mStartRecording;
			break;
		case R.id.playButton:
			onPlay(mStartPlaying);
			if (mStartPlaying) {
				PlayButton.setText(" Stop Playing ");
			} else {
				PlayButton.setText("Play Recording");
			}
			mStartPlaying = !mStartPlaying;
			break;
		case R.id.submitButton:
			_progressDialog = ProgressDialog.show(WhatDidTheySay.this, "",
					"Translating Audio. Please wait...", true);
			_computeResult = new Thread() {
				public void run() {
					WebService server = new WebService(WhatDidTheySay.this);
					try {
						Log.d("PHILMONROE", mFileName);
						_result.putExtra(
								WebService.WHAT_DID_THEY_SAY_RESULT_KEY, server
										.whatDidTheySay(mFileName));
						_result.putExtra("audioPath", mFileName);

						WhatDidTheySay.this.runOnUiThread(new Runnable() {
							public void run() {
								_progressDialog.dismiss();
								startActivity(_result);
								_computeResult = null;
								finish();
							}
						});
					} catch (Throwable e) {
						e.printStackTrace();
						WhatDidTheySay.this.runOnUiThread(new Runnable() {
							public void run() {
								_progressDialog.dismiss();
								Toast.makeText(WhatDidTheySay.this,
										"A server error occured... X(",
										Toast.LENGTH_LONG).show();
								finish();
							}
						});
					}
				}
			};
			_computeResult.start();
			break;
		}
	}

	@Override
	public void onPause() {
		super.onPause();
		if (mRecorder != null) {
			mRecorder.release();
			mRecorder = null;
		}

		if (mPlayer != null) {
			mPlayer.release();
			mPlayer = null;
		}
		finish();
	}

}